package com.community.web.user.controller.wallet;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.community.common.exception.NeusoftException;
import com.community.common.login.LoginUserHolder;
import com.community.common.result.Result;
import com.community.common.result.ResultCodeEnum;
import com.community.model.entity.PortalWalletLog;
import com.community.model.entity.User;
import com.community.model.entity.Wallet;
import com.community.web.user.service.WalletService;
import com.community.web.user.vo.TransferVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Api(tags = "钱包模块")
@RestController
@RequestMapping("/user/wallet")
public class WalletController {
    @Autowired
    private WalletService service;


    @ApiOperation("设置支付密码")
    @PutMapping("set")
    public Result setPassword(@RequestBody String password){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        password = DigestUtils.md5Hex(password);
        LambdaUpdateWrapper<Wallet> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Wallet::getUserId, userId);
        updateWrapper.set(Wallet::getWalletPassword, password);
        service.update(updateWrapper);
        return Result.ok();
    }

    @ApiOperation("校验支付密码")
    @PostMapping()
    public Result<Wallet> password(@RequestBody String password){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        //返回钱包余额
        Wallet wallet = service.select(userId, password);
        wallet.setWalletPassword("");
        return Result.ok(wallet);
    }

    @ApiOperation("获取钱包信息")
    @GetMapping()
    public Result<Wallet> getWalletInfo(){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wallet::getUserId, userId);
        Wallet wallet = service.getOne(queryWrapper);
        return Result.ok(wallet);
    }

    @ApiOperation("充值余额")
    @PutMapping("{money}")
    public Result addMoney(@PathVariable BigDecimal money){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        service.addMoney(userId, money);
        return Result.ok();
    }

    @ApiOperation("转账")
    @PutMapping("transfer")
    public Result transferMoney(@RequestBody TransferVo transferVo){
        //判断目标是否自身
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        if (userId.intValue() == transferVo.getUserId()){
            throw new NeusoftException(ResultCodeEnum.SELF_ERROR);
        }
        service.transfer(userId, transferVo);
        return Result.ok();
    }

    @ApiOperation("根据用户id发送验证码")
    @GetMapping("sendCode")
    public Result sendCode(){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        service.sendCodeById(userId);
        return Result.ok();
    }

}
